package Aleehot100;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: c041
 * Package: Aleehot100
 * Description:102. 二叉树的层序遍历
 *
 * @Author BCXJ
 * @Create 2025/7/9 08:55
 * @Version 1.0
 * @Since 1.0
 */
public class c041 {
    public static void main(String[] args) {

    }
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    // 递归实现
    static List<List<Integer>> res = new ArrayList<>();
    public static List<List<Integer>> levelOrder3(TreeNode root) {
        res.clear();
        process(root, 0);
        return res;
    }

    private static void process(TreeNode node, int i) {
        if(node == null) {
            return;
        }
        if(res.size() < i) {
            res.add(new ArrayList<>());
        }

        // 添加元素
        res.get(i - 1).add(node.val);
        process(node.left, i + 1);
        process(node.right, i + 1);
    }
}







/*
class Solution {

    // 递归实现
    public static List<List<Integer>> levelOrder3(TreeNode root) {
        List<List<Integer>> ans = new ArrayList<>();
        process(root, ans, 0);
        return ans;
    }

    private static void process(TreeNode root, List<List<Integer>> ans, int level) {
        if (root == null) {
            return;
        }
        if (ans.size() == level) {
            ans.add(new ArrayList<>());
        }
        ans.get(level).add(root.val);
        process(root.left, ans, level + 1);
        process(root.right, ans, level + 1);
    }

    // 提交时把方法名改为levelOrder，此方法为普通bfs，此题不推荐
    public static List<List<Integer>> levelOrder1(TreeNode root) {

        // ans队列
        ArrayList<List<Integer>> ans = new ArrayList<>();
        if (root == null) {
            return ans;
        }
        // 队列
        Queue<TreeNode> queue = new LinkedList<>();
        HashMap<TreeNode, Integer> map = new HashMap<>(); // key 为 val，value为level(层数)

        queue.add(root);
        map.put(root, 0);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            Integer level = map.get(node);
            if (ans.size() == level) {
                ArrayList<Integer> childAns = new ArrayList<>();
                ans.add(childAns);
            }
            ans.get(level).add(node.val);
            if (node.left != null) {
                queue.add(node.left);
                map.put(node.left, level + 1);
            }
            if (node.right != null) {
                queue.add(node.right);
                map.put(node.right, level + 1);
            }
        }
        return ans;
    }

    // 数组队列解决

    public static int intMAXLEN = 2001;
    public static TreeNode[] queue = new TreeNode[intMAXLEN];
    public static int r, l;

    public static List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ans = new ArrayList<>();
        if (root == null) {
            return ans;
        }

        queue[0] = root;
        l = 0;
        r = 1;
        int n = 0;
        while (l < r) {
            n = r - l;
            ArrayList<Integer> childAns = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                TreeNode cur = queue[l++];
                if (cur.left != null) {
                    queue[r] = cur.left;
                    r++;
                }
                if (cur.right != null) {
                    queue[r] = cur.right;
                    r++;
                }
                childAns.add(cur.val);
            }
            ans.add(childAns);
        }

        return ans;
    }
}

 */